import { Provide} from '@midwayjs/core';
import { Announcement } from '../../entity/user/announcement';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';


@Provide()
export class AnnService {

  @InjectEntityModel(Announcement)
  AnnModel:Repository<Announcement>

  async getAllAnn():Promise<Announcement[]>{
    try{
      const result = await this.AnnModel.find()
      console.log("result",result);
      return result
    }catch(err){
      console.log("err",err);    
    }
  }

  async addAnn(info):Promise<Announcement>{
    try{
      const result = await this.AnnModel.save(info)
      console.log("result",result);
      return result
    }catch(err){
      console.log("err",err);    
    }
  }
  async updateAnn(info):Promise<Announcement>{
    try{
        const data = await this.AnnModel.findOne({
            where:{
                id:info.id
            }
          })
        data.title=info.title
        data.content=info.content
        data.image=info.image
        const result = await this.AnnModel.save(data)
      return result
    }catch(err){
      console.log("err",err);    
    }
  }

  async deleteAnn(id){
    try{
        const result = await this.AnnModel.delete(id)   
        return result
    }catch(err){
      console.log("err",err);  
    }
  }
}